package com.foreveross;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.foreveross.crawl.common.exception.BusinessException;
import com.foreveross.crawl.vo.ChannelInfo;
import com.foreveross.crawl.vo.TaskGroup;

public class JsonStringOperator {
	
	public static Logger logger = Logger.getLogger(JsonStringOperator.class);

	/**
	 * 根据key返回值
	 * @param obj
	 * @param key
	 * @return
	 * @throws BusinessException 
	 */
	public static Object getObjectByKey(Object obj, String key) throws BusinessException{
		JSONObject jsonObject = JSONObject.fromObject(obj);
		if(!jsonObject.containsKey(key)){
			throw new BusinessException("解析json时找不到指定的key: " + key);
		}
		return jsonObject.get(key);
	}
	
	/**
	 * 检测返回的json数据中是否保护错误信息
	 * @param obj 返回的json串
	 * @param keys 错误key  如：error等
	 * @return
	 */
	public static boolean containsErrorInfo(Object obj, String[] keys){
		JSONObject jsonObject = JSONObject.fromObject(obj);
		for(String key : keys){
			if(jsonObject.containsKey(key)){
				logger.info("返回的json信息包含错误{}" + jsonObject.get(key));
				return true;
			}
		}
		return false;
	}
	
	/*public static JSONArray getJsonArrayByKey(Object obj) throws BusinessException{
		JSONArray jsonArray = JSONArray.fromObject(obj);
		if(jsonArray.contains(key)){
			throw new BusinessException("解析json时找不到指定的key: " + key);
		}
		return jsonArray.get
	}*/
	
	@Deprecated
	public static List<TaskGroup> analyzeGroupInfoByJsonStr(String jsonStr) throws BusinessException{
		List<TaskGroup> retList = new ArrayList<TaskGroup>();
		Object obj = getObjectByKey(jsonStr, "data");
		JSONArray jsonArray = JSONArray.fromObject(obj);
		TaskGroup taskGroup = null;
		for(Object jsonObj : jsonArray){
			taskGroup = new TaskGroup();
			taskGroup.setId(Long.parseLong(getObjectByKey(jsonObj, "id").toString()));
			taskGroup.setName((String)getObjectByKey(jsonObj, "name"));
			retList.add(taskGroup);
		}
		return retList;
	}
	
	/**  
	 * @Description: 获取jsonStr字符串中所有的渠道ID信息 —— 因为兼容版本问题，目前取的是remarks字段的值
	 * @param jsonStr
	 * @return
	 * @throws BusinessException
	 * @author luofangyi
	 * @date 2014-6-27 上午10:38:28 
	 */ 
	public static List<Long> analyzeAllChannelIdByJson(String jsonStr) throws BusinessException{
		List<Long> retList = new ArrayList<Long>();
		Object obj = getObjectByKey(jsonStr, "data");
		JSONArray jsonArray = JSONArray.fromObject(obj);
		for(Object jsonObj : jsonArray){
			try {
				Long channelId = Long.parseLong(getObjectByKey(jsonObj, "remarks").toString().trim());
				retList.add(channelId);
			} catch (Exception e) {
				logger.error("解析从服务控制台返回的渠道ID时，发生异常!" + e.getMessage());
			}
		}
		return retList;
	}
	
	/**  
	 * @Description: 获取jsonStr字符串中所有的渠道(ID、NAME)信息 —— 因为兼容版本问题，目前取的是remarks字段的值
	 * @param jsonStr
	 * @return
	 * @throws BusinessException
	 * @author luofangyi
	 * @date 2014-7-15 下午5:26:20 
	 */ 
	public static Map<Long, String> analyzeAllChannelByJson(String jsonStr) throws BusinessException{
		Map<Long, String> retMap = new HashMap<Long, String>();
		Object obj = getObjectByKey(jsonStr, "data");
		JSONArray jsonArray = JSONArray.fromObject(obj);
		for(Object jsonObj : jsonArray){
			try {
				Long channelId = Long.parseLong(getObjectByKey(jsonObj, "remarks").toString().trim());
				String channelName = getObjectByKey(jsonObj, "name").toString().trim();
				retMap.put(channelId, channelName);
			} catch (Exception e) {
				logger.error("解析从服务控制台返回的渠道ID时，发生异常!" + e.getMessage());
			}
		}
		return retMap;
	}
	
	/**  
	 * @Description: 获取jsonStr字符串中所有的渠道详细信息 —— 因为兼容版本问题，目前取的是remarks字段的值
	 * @param jsonStr
	 * @return
	 * @throws BusinessException
	 * @author luofangyi
	 * @date 2014-7-21 上午11:55:38 
	 */ 
	public static Map<Long, ChannelInfo> analyzeAllChannelDetailByJson(String jsonStr) throws BusinessException{
		Map<Long, ChannelInfo> retMap = new HashMap<Long, ChannelInfo>();
		Object obj = getObjectByKey(jsonStr, "data");
		JSONArray jsonArray = JSONArray.fromObject(obj);
		for(Object jsonObj : jsonArray){
			try {
				Object remarks = getObjectByKey(jsonObj, "remarks");
				Object groupObj = getObjectByKey(jsonObj, "group");
				Long channelId = Long.parseLong(remarks != null ? remarks.toString().trim() : "0");
				String channelName = getObjectByKey(jsonObj, "name").toString().trim();
				String channelFullName = getObjectByKey(jsonObj, "fullName").toString().trim();
				String dbTable = getObjectByKey(jsonObj, "dbTable").toString().trim();
				String dbType = getObjectByKey(jsonObj, "dbType").toString().trim();
				String dbTypeName = getObjectByKey(jsonObj, "dbTypeName").toString().trim();
				String isUseProxy = getObjectByKey(jsonObj, "isUseProxy").toString().trim();
				Long groupId = Long.parseLong(groupObj != null ? groupObj.toString().trim() : "0");
				String groupName = getObjectByKey(jsonObj, "groupName").toString().trim();
				retMap.put(channelId, new ChannelInfo(channelId, channelName, channelFullName, dbTable, dbType, dbTypeName, isUseProxy, groupId, groupName));
			} catch (Exception e) {
				logger.error("解析从服务控制台返回的渠道ID时，发生异常!" + e.getMessage());
			}
		}
		return retMap;
	}
	
	public static void main(String[] args){
		Object a = "1";
		String b = "bb";
		Long c = (Long) a;
		if(c instanceof Long){
			System.out.println(a);
		} else {
			System.out.println("nothing");
		}
	}
}
